// pages/myself/merchant/MerchantBrand.js
let network = null;
let utils = null;
let api = null;
const app = getApp();
var start = 0;
var Zan = require('../../../dist/index');
let WxNotificationCenter = require('../../../utils/WxNotificationCenter.js');

Page(Object.assign({}, Zan.TopTips, {

  /**
   * 页面的初始数据
   */
  data: {
    tenantInfo: null,
    myBrandList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();

    // 注册列表刷新事件
    WxNotificationCenter.addNotification("RefreshMerchantBrand", this.refreshNotification, this)

    // 获取商户信息
    network._post_json(this, api.getTenant, {}, success);
    this.onPullDownRefresh()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    WxNotificationCenter.removeNotification("RefreshMerchantBrand", this);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.setData({
      myBrandList: []
    })
    network._post_page_json(this, api.myBrandList, start, { status: 1 }, Success);

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  /**
   * 进入商户资料修改
   */
  goModifyTenant: function (e) {
    if (utils.canTap(e)) {
      wx.navigateTo({
        url: '/pages/myself/merchant/ModifyTenant?tenantInfo=' + JSON.stringify(this.data.tenantInfo)
      })
    }
  },

  /**
  * 添加品牌
  */
  goAddBrand: function (e) {
    if (utils.canTap(e)) {
      wx.navigateTo({
        url: '/pages/myself/merchant/AddBrand',
      })
    }
  },

  /**
   * 到期续费
   */
  goBrandRenew: function (e) {
    let brandId = e.currentTarget.dataset.brandid;
    let type = e.currentTarget.dataset.type;
    // if (type == 1) {
    //   type = 3
    // } else if (type == 2) {
    //   type = 1
    // } else if (type == 3) {
    //   type = 2
    // }
    if (utils.canTap(e)) {
      wx.navigateTo({
        url: '/pages/myself/merchant/BrandRenew?brandId=' + brandId + '&type=' + type,
      })
    }
  },

  /**
   * 入进VIP升级
   */
  goUpgradeVIP: function (e) {
    var brandId = e.currentTarget.dataset.brandid
    if (utils.canTap(e)) {
      wx.navigateTo({
        url: '/pages/myself/merchant/UpgradeVIP?brandId=' + brandId,
      })
    }
  },

  /**
   * 入进品牌
   */
  goMerchantCoupon: function (e) {
    var brandId = e.currentTarget.dataset.brandid;
    var name = e.currentTarget.dataset.name;
    var img = e.currentTarget.dataset.img;
    var count = e.currentTarget.dataset.count;
    var tenantType = e.currentTarget.dataset.tenanttype;
    var type = e.currentTarget.dataset.type;
    var end = e.currentTarget.dataset.end;
    if (utils.canTap(e)) {
      wx.navigateTo({
        url: '/pages/myself/merchant/MerchantCoupon?BrandId=' + brandId + '&name=' + encodeURIComponent(name) + '&img=' + img + '&count=' + count + '&index=0' + '&type=' + type + '&tenantType=' + tenantType + '&end=' + utils.formatDate02(new Date(end))
      })
    }
  },

  /**
  * 刷新地址
  */
  refreshNotification: function () {
    this.setData({
      myBrandList: []
    })
    // 获取商户信息
    network._post_json(this, api.getTenant, {}, success);
    network._post_page_json(this, api.myBrandList, start, { status: 1 }, Success);
  }
}))

// 获取商户信息成功
var success = function (that, res) {
  if (res.data.data) {
    that.setData({
      tenantInfo: res.data.data
    })
  }

}


var Success = function (that, res) {

  if (res.data.data) {
    that.setData({
      myBrandList: res.data.data
    })
  }

}